package com.yungu.swift.common.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * 发送短信验证码工具类
 *
 */
@Slf4j
public class SendSmsUtil {

    // 签名
    private static final String signName = "老兵出行";
    // 模板
    private static final String templateCode = "SMS_295335938"; //您的验证码为：${code}，请勿泄露于他人！
    private static final String templateAmount = "SMS_465395574"; //您有一个用车订单还未支付，共[${amount}]元，请尽快到应用内支付。
    private static final String templatePass = "SMS_465895197"; //司机审核成功 尊敬的用户您好！您的司机申请已通过审核，初始密码为${password}，请您在登录后修改密码，感谢您的信任与支持，祝您生活愉快，一切顺利！
    private static final String templateReject = "SMS_465945202"; //司机审核失败 尊敬的用户您好！您的司机申请未通过审核，请您根据驳回原因进行相应调整和完善，并重新提交申请。感谢您的理解和配合，祝您生活愉快，一切顺利！

    // 阿里云短信配置信息
    private static final String accessKeyId = "LTAI5tHThvA5nvMkSuCQquvm";
    private static final String accessKeySecret = "ICuniReDo8UBSOIQBA5K5cVPso21kP";
    private static final String REGION_ID = "cn-jinan";
    private static final String PRODUCT = "Dysmsapi";
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    /**
     * 发送短信通知
     *
     * @param phone  手机号   国际短信 （区号+手机号）
     * @param code 验证码
     * @param type 根据类型发送不同短信模板
     * @return 执行结果
     */
    public static boolean smsSend(String phone,String code,String type) {
        try {
            IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            request.setMethod(MethodType.POST);
            // 手机号可以单个也可以多个（多个用逗号隔开，如：15*******13,13*******27,17*******56）
            request.setPhoneNumbers(phone);
            request.setSignName(signName);
            if(type.equals("0")) {
                //验证码
                request.setTemplateCode(templateCode);
                request.setTemplateParam("{\"code\":\""+ code +"\"}");
            } else if (type.equals("1")) {
                //  司机催款
                request.setTemplateCode(templateAmount);
                request.setTemplateParam("{\"amount\":\""+ code +"\"}");
            } else if(type.equals("2")) {
                //  司机申请通过
                request.setTemplateCode(templatePass);
                request.setTemplateParam("{\"password\":\""+ code +"\"}");
            } else if(type.equals("3")) {
                //  司机申请驳回
                request.setTemplateCode(templateReject);
            }

            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            System.out.println(sendSmsResponse.getMessage()+sendSmsResponse.getCode());
            if ((sendSmsResponse.getCode() != null) && (sendSmsResponse.getCode().equals("OK"))) {
                log.info("发送成功,code:" + sendSmsResponse.getCode());
                return true;
            } else {
                log.info("发送失败,code:" + sendSmsResponse.getCode());
                return false;
            }
        } catch (ClientException e) {
            log.error("发送失败,系统错误！", e);
            return false;
        }
    }
}
